package org.jeecg.modules.operations.assistant.xcbd.job;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wechat.pay.java.core.exception.ServiceException;
import com.wechat.pay.java.service.payments.model.Transaction;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.operations.assistant.vehicle.entity.VehicleInfo;
import org.jeecg.modules.operations.assistant.vehicle.service.IVehicleInfoService;
import org.jeecg.modules.operations.assistant.vehicle.util.ApiServiceUtil;
import org.jeecg.modules.operations.assistant.wechat.service.QuickStart;
import org.jeecg.modules.operations.assistant.xcbd.entity.XcbdCollectionRecord;
import org.jeecg.modules.operations.assistant.xcbd.entity.XcbdSalesContract;
import org.jeecg.modules.operations.assistant.xcbd.mapper.XcbdCollectionRecordMapper;
import org.jeecg.modules.operations.assistant.xcbd.mapper.XcbdSalesContractMapper;
import org.jeecg.modules.system.entity.SysAnnouncementSend;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

@Slf4j
public class CollectionRecordJob implements Job {
    @Autowired
    private XcbdCollectionRecordMapper xcbdCollectionRecordMapper;

    @Autowired
    private XcbdSalesContractMapper xcbdSalesContractMapper;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        log.info(String.format(" Jeecg-Boot 发送消息任务 CollectionRecordJob !  时间:" + DateUtils.getTimestamp()));
        LambdaQueryWrapper<XcbdCollectionRecord> queryWrapper = new LambdaQueryWrapper<XcbdCollectionRecord>();
        queryWrapper.eq(XcbdCollectionRecord::getPaymentMethod, "2");   //付款方式 2 微信
//        queryWrapper.eq(XcbdCollectionRecord::getReceiptStatus, 0);     // 未到账
        queryWrapper.eq(XcbdCollectionRecord::getTradeState, "NOTPAY");     // 订单未支付
//        String[] tradeStateArray = {"SUCCESS","REFUND","CLOSED","REVOKED","PAYERROR"};
//        Collection<String> tradeStates = Arrays.asList(tradeStateArray);
//        queryWrapper.notIn(XcbdCollectionRecord::getTradeState, tradeStates);
        List<XcbdCollectionRecord> collectionRecordList = xcbdCollectionRecordMapper.selectList(queryWrapper);
        if(collectionRecordList != null && collectionRecordList.size()>0) {
            for (XcbdCollectionRecord collectionRecord : collectionRecordList) {
                log.info(String.format(" Jeecg-Boot 发送消息任务 CollectionRecordJob 查询到数据 !  时间:" + DateUtils.getTimestamp()));
                try {
                    Transaction transaction = QuickStart.queryOrderByOutTradeNo(collectionRecord.getOrderNumber());
                    String tradeState = transaction.getTradeState().name();
//                    if(tradeState.equals("SUCCESS")) {  // SUCCESS：交易成功。
//                        XcbdSalesContract xcbdSalesContract = xcbdSalesContractMapper.selectById(collectionRecord.getSalesContractId());
//                        BigDecimal amountCollected = collectionRecord.getAmountCollected();
//                        BigDecimal collectionAmount = xcbdSalesContract.getCollectionAmount();
    //                    collectionRecord.setReceiptStatus(1);  // 到账成功
//                        collectionRecord.setTradeState(tradeState);
//                        collectionRecord.setTradeStateDesc(transaction.getTradeStateDesc());
//                        collectionAmount = collectionAmount.subtract(amountCollected);
//                        xcbdSalesContract.setCollectionAmount(collectionAmount); // 待回款金额
    //                    xcbdSalesContractMapper.updateById(xcbdSalesContract);
//                    } else {
                        collectionRecord.setTradeState(tradeState);
                        collectionRecord.setTradeStateDesc(transaction.getTradeStateDesc());
//                    }

                } catch (ServiceException e) {
//                    collectionRecord.setTradeState(e.getErrorCode());
//                    collectionRecord.setTradeStateDesc(e.getErrorMessage());
                    log.error("ServiceException:{},{}", e.getErrorCode(), e.getErrorMessage());
                }
                xcbdCollectionRecordMapper.updateById(collectionRecord);
            }
        }
    }
}